package com.project.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.ColorVo;
import com.project.bean.ProductBean;
import com.project.bean.ProductESBean;
import com.project.bean.SizeVo;
import com.project.dao.IProductDao;
import com.project.dto.BeautifyProductDto;
import com.project.dto.WaitProductDto;
import com.project.mapper.IProductMapper;
import com.project.dto.WaitProductDtoWeb;
import com.project.service.IProductService;
import com.project.service.IWaitProductService;
import com.project.util.QiniuCloudUtil;
import com.project.util.SnowIdUtils;
import com.project.vo.ImgPriceVo;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("product")
public class ProductController {
      @Autowired
      RabbitTemplate rabbitTemplate;
      @Autowired
      private IProductService service;
      @Autowired
      private IProductService productService;
      @Autowired
      private IProductDao productDao;
      @Autowired
      private IWaitProductService waitProductService;
      @Autowired
      private IProductMapper mapper;
      
      @RequestMapping("findProductInfo")
      public ProductBean findProductInfo(String productNum){
            ProductBean productBean=productService.findProductInfo(productNum);
            String img= (String) QiniuCloudUtil.QiniuCloudDownloadImage(productBean.getProductESBean().getImg());
            productBean.getProductESBean().setImg(img);
            return productBean;
      }
      
      
      
      @RequestMapping("findSizeByProductName")
      public List<SizeVo> findSizeByProductName(String productName) {
            return productService.findSizeByProductName(productName);
      }
      @RequestMapping("findColorByNameSize")
      public List<ColorVo> findColorByNameSize(String productName, String productSize){
            return productService.findColorByNameSize(productName,productSize);
            
      }
      @RequestMapping("findImgByNameSizeColor")
      public ImgPriceVo findImgByNameSizeColor(String productName, String productSize, String productColor) {
            ImgPriceVo vo=productService.findImgByNameSizeColor(productName, productSize, productColor);
            System.out.println(vo.getImg());
            String img= (String) QiniuCloudUtil.QiniuCloudDownloadImage(vo.getImg());
            vo.setImg(img);
            System.out.println(vo);
            return vo;
      }
      @RequestMapping("findPTByType")
      public List<ProductBean> findPTByType(String type){
            List<ProductBean> productBeans=service.findPTByType(type);
            productBeans.forEach(n->{
                 String img= (String) QiniuCloudUtil.QiniuCloudDownloadImage(n.getProductESBean().getImg());
                 n.getProductESBean().setImg(img);
            });
            return productBeans;
      }
      @RequestMapping("findCXByType")
      public List<ProductBean> findCXByType(String type){
            List<ProductBean> productBeans=service.findCXByType(type);
            productBeans.forEach(n->{
                  String img= (String) QiniuCloudUtil.QiniuCloudDownloadImage(n.getProductESBean().getImg());
                  n.getProductESBean().setImg(img);
            });
            return productBeans;
      }
      @RequestMapping("findQXByType")
      public List<ProductBean> findQXByType(String type){
            List<ProductBean> productBeans=service.findQXByType(type);
            productBeans.forEach(n->{
                  String img= (String) QiniuCloudUtil.QiniuCloudDownloadImage(n.getProductESBean().getImg());
                  System.out.println(img);
                  n.getProductESBean().setImg(img);
            });
            return productBeans;
      }
      
      /**
       * 查询所有商品类型（不重复）
       * @return
       */
      @RequestMapping("findAllType")
      public List<String> findAllType(){
            return service.findAllType();
      }
      @RequestMapping("keyWord")
      public List<ProductESBean> keyWord(String ms) {
            List<ProductESBean> list=service.keyWord(ms);
            list.forEach(n->{
                  String path= (String) QiniuCloudUtil.QiniuCloudDownloadImage(n.getImg());
                  n.setImg(path);
            });
            return list;
      }
      
      
      /**
       * 获取需要上架的商品
       */
      @RequestMapping("getBeautify")
      public String getBeatifyPro(String name,String proType){
            //调后端接口findProduct
            List<WaitProductDtoWeb> list=waitProductService.findWaitProductByTypeAndName(proType,name);
            List<ProductBean> productBeans=new ArrayList<>();
            list.forEach(n->{
                  ProductBean productBean=new ProductBean();
                  productBean.setProductNum(n.getProductId());
                  productBean.setNum(n.getNum());
                  productBean.setProductName(n.getProductName());
                  productBean.setColor(n.getColor());

                  productBean.setProductStatus(n.getProductStatus());

                  productBean.setProductType(n.getProductType());
                  productBean.setSize(n.getSize());
                  productBean.setPurchasingPrice(n.getInPrice());
                  productBeans.add(productBean);
            });
            productBeans.forEach(n->{
                  ProductBean productBean=service.findByNameAndColorAndSize(n.getProductName(),n.getColor(),n.getSize());
                  if(productBean!=null){
                        if(productBean.getNum()<=0){
                              QueryWrapper<ProductBean> queryWrapper=new QueryWrapper<>();
                              queryWrapper.eq("productNum",productBean.getProductNum());
                              ProductBean productBean1=mapper.selectOne(queryWrapper);
                              productBean1.setNum(productBean.getNum());
                              mapper.updateById(productBean1);
                        }
                        productBeans.remove(n);
                  }
            });
            //将获取的商品存入数据库
            if(productBeans.size()!=0){
                  productService.addPro(productBeans);
            }
            return "ok";
      }
      /**
       * 通过mq将美化后的商品发送给后端
       * @param productBean
       * @return
       */
      @RequestMapping("sendPro")
      public String sendPro(ProductBean productBean,String ms, MultipartFile file) throws Exception {
            System.out.println(productBean);
            System.out.println(ms);
            //rabbitTemplate.convertAndSend("", "", productBean);
            ProductESBean proSe=productBean.getProductESBean();
            String fileName= (String) QiniuCloudUtil.QiniuCloudUploadImage(file);
            //6d6f2f8fa8a343248f86506c431bab20.png
            System.out.println(fileName);
            proSe.setImg(fileName);
            productService.updateProductStatus(productBean.getProductNum(),4,productBean.getSellPrice());
            proSe.setProductNum(productBean.getProductNum());
            proSe.setProductName(productBean.getProductName());
            proSe.setDescribe(ms);
            productDao.save(proSe);
            BeautifyProductDto productDto=new BeautifyProductDto();
            productDto.setProductNum(productBean.getProductNum());
            productDto.setDescribe(ms);
            productDto.setSellPrice(productBean.getSellPrice());
            System.out.println(productDto);
            String jsonString = JSONObject.toJSONString(productDto);
            rabbitTemplate.convertAndSend("webOne","beautifyProduct",jsonString);
            return "ok";
      }
      @RequestMapping("findAllBeautify")
      public IPage<ProductBean> findAllBeautify(Integer pageNO,String productNum){
            return productService.findByBeautify(pageNO,productNum);
      }
      @RequestMapping("findByNameAndColorAndSize")
      public ProductBean findByNameAndColorAndSize(String name,String color,String size){
            ProductBean productBean=service.findByNameAndColorAndSize(name,color,size);
            /*String img= (String) QiniuCloudUtil.QiniuCloudDownloadImage(productBean.getProductESBean().getImg());
            productBean.getProductESBean().setImg(img);*/
            return productBean;
      }
      
      /**
       * 后端需要的接口
       * @param productStatus
       * @param productNum
       * @return
       */
      @RequestMapping("updateProductStatus/{productStatus}/{productNum}")
      public String updateProductStatus(@PathVariable("productStatus") int productStatus, @PathVariable("productNum")String productNum){
            service.updateProductStatus(productStatus,productNum);
            return "ok";
      }
      @RequestMapping("findById")
      public ProductBean findById(String productNum) {
            return service.findById(productNum);
      }
      
}
